package com.cz.nongyetongv5.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.EntDetailActivity;
import com.cz.nongyetongv5.activity.EntInfoPicDetailActivity;
import com.cz.nongyetongv5.activity.SinglePicActivity;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.EntPic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangliang on 2017/6/26.
 */

public class EntInfoPhoneFragment extends Fragment {


    private RecyclerView lv_patrol_pic;
    private PhotoAdapter adapter;
    private View view;
    private List<EntPic> entpicList;

    public static EntInfoPhoneFragment newInstance() {
        EntInfoPhoneFragment fragment = new EntInfoPhoneFragment();
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ent_phone, null);
        lv_patrol_pic = view.findViewById(R.id.lv_patrol_pic);
        init();
        return view;
    }

    public void init() {
        entpicList = new ArrayList<>();
        adapter = new PhotoAdapter(entpicList);
        //        adapter = new PhotoAdapter(((EntDetailActivity) getActivity()).getEntPics());
        View notDataView = getActivity().getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) lv_patrol_pic.getParent(), false);
        adapter.setEmptyView(notDataView);
        lv_patrol_pic.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        lv_patrol_pic.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //                Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                //                intent.putExtra("position", position);
                //                intent.putExtra("list", (Serializable) entpicList);
                //                startActivity(intent);

                if (((EntDetailActivity) getActivity()).getEntBean().getKind().equals("7")) {

                    if (entpicList.get(position).getItemType() == 1) {
                        List<EntPic> tmpList = new ArrayList<>();
                        for (EntPic entPic : entpicList) {
                            if (entPic.getItemType() == 1) {
                                tmpList.add(entPic);
                            }
                        }
                        if (entpicList.size()>tmpList.size()){
                            Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                            intent.putExtra("position", position-3);
                            intent.putExtra("list", (Serializable) tmpList);
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                            intent.putExtra("position", position);
                            intent.putExtra("list", (Serializable) tmpList);
                            startActivity(intent);
                        }
                    } else {
                        if (position == 0 || position == 1 || position == 2) {
                            Intent intent = new Intent(getActivity(), SinglePicActivity.class);
                            intent.putExtra("kind",((EntDetailActivity) getActivity()).getEntBean().getKind());
                            intent.putExtra("showBottom", false);
                            intent.putExtra("type", position + 1);
                            intent.putExtra("url", entpicList.get(position).getPath());
                            startActivity(intent);
                        }
                    }

                    //                    if (position == 0 || position == 1 || position == 2) {
                    //                        Intent intent = new Intent(getActivity(), SinglePicActivity.class);
                    //                        intent.putExtra("showBottom",false);
                    //                        intent.putExtra("type", position + 1);
                    //                        intent.putExtra("url",  entpicList.get(position).getPath());
                    //                        startActivity(intent);
                    //                    }else {
                    //                        List<EntPic> tmpList = new ArrayList<>();
                    //                        for (EntPic entPic :  entpicList) {
                    //                            if (entPic.getItemType() == 1) {
                    //                                tmpList.add(entPic);
                    //                            }
                    //                        }
                    //                        Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                    //                        intent.putExtra("position", position - 3);
                    //                        intent.putExtra("list", (Serializable) tmpList);
                    //                        startActivity(intent);
                    //                    }
                } else {
                    if (entpicList.get(position).getItemType() == 1) {
                        List<EntPic> tmpList = new ArrayList<>();
                        for (EntPic entPic : entpicList) {
                            if (entPic.getItemType() == 1) {
                                tmpList.add(entPic);
                            }
                        }
                        if (entpicList.size()>tmpList.size()){
                            Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                            intent.putExtra("position", position-4);
                            intent.putExtra("list", (Serializable) tmpList);
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                            intent.putExtra("position", position);
                            intent.putExtra("list", (Serializable) tmpList);
                            startActivity(intent);
                        }
                    } else {
                        if (position == 0 || position == 1 || position == 2 || position == 3) {
                            Intent intent = new Intent(getActivity(), SinglePicActivity.class);
                            intent.putExtra("showBottom", false);
                            intent.putExtra("kind",((EntDetailActivity) getActivity()).getEntBean().getKind());
                            intent.putExtra("type", position);
                            intent.putExtra("url", entpicList.get(position).getPath());
                            startActivity(intent);
                        }
                    }

                    //                    if (position == 0 || position == 1 || position == 2 || position == 3) {
                    //                        Intent intent = new Intent(getActivity(), SinglePicActivity.class);
                    //                        intent.putExtra("showBottom",false);
                    //                        intent.putExtra("type", position);
                    //                        intent.putExtra("url", entpicList.get(position).getPath());
                    //                        startActivity(intent);
                    //                    }else {
                    //                        List<EntPic> tmpList = new ArrayList<>();
                    //                        for (EntPic entPic : entpicList) {
                    //                            if (entPic.getItemType() == 1) {
                    //                                tmpList.add(entPic);
                    //                            }
                    //                        }
                    //                        Intent intent = new Intent(getActivity(), EntInfoPicDetailActivity.class);
                    //                        intent.putExtra("position", position - 4);
                    //                        intent.putExtra("list", (Serializable) tmpList);
                    //                        startActivity(intent);
                    //                    }
                }
            }
        });

        entpicList.addAll( ((EntDetailActivity) getActivity()).getEntPics());
//        if (((EntDetailActivity) getActivity()).getEntBean().getCharge_pic() == null || ((EntDetailActivity) getActivity()).getEntBean().getCharge_pic().equals("")) {
        //
        //            for (EntPic entPic : ((EntDetailActivity) getActivity()).getEntPics()) {
        //                if (entPic.getItemType() == 1) {
        //                    entpicList.add(entPic);
        //                }
        //            }
        //        } else {
        //            for (EntPic entPic : ((EntDetailActivity) getActivity()).getEntPics()) {
        //                entpicList.add(entPic);
        //            }
        //        }
    }


    public class PhotoAdapter extends BaseMultiItemQuickAdapter<EntPic, BaseViewHolder> {
        //        public PhotoAdapter() {
        //            super(R.layout.product_pic_item, ((EntAddActivity) getActivity()).getEntPics());
        //        }


        public PhotoAdapter(List data) {
            super(data);
            addItemType(EntPic.IMG_1, R.layout.product_pic_item);
            addItemType(EntPic.IMG_2, R.layout.product_pic_item_2);
        }


        @Override
        protected void convert(BaseViewHolder helper, EntPic item) {
            switch (helper.getItemViewType()) {
                case EntPic.IMG_1:
                    Glide.with(mContext)
                            .load(InitApplication.BASE_URL_ENT_PIC+item.getPath()).placeholder(R.drawable.loading)
                            .into((ImageView) helper.getView(R.id.iv_iv));
                    break;
                case EntPic.IMG_2:
                    if (((EntDetailActivity) getActivity()).getEntBean().getKind().equals("7")) {
                        if (helper.getLayoutPosition() == 0) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_charge);
                                helper.setText(R.id.tv_name, "手持身份证正面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "手持身份证正面");
                            }
                        } else if (helper.getLayoutPosition() == 1) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_card_face);
                                helper.setText(R.id.tv_name, "身份证正面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "身份证正面");
                            }

                        } else if (helper.getLayoutPosition() == 2) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_card_back);
                                helper.setText(R.id.tv_name, "身份证反面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "身份证反面");
                            }
                        }
                    } else {
                        if (helper.getLayoutPosition() == 0) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_business_licence);
                                if (((EntDetailActivity) getActivity()).getEntBean().getKind().equals("6")){
                                    helper.setText(R.id.tv_name, "资格认定证照(暂无)");
                                }else {
                                    helper.setText(R.id.tv_name, "营业执照(暂无)");
                                }

                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                if (((EntDetailActivity) getActivity()).getEntBean().getKind().equals("6")){
                                    helper.setText(R.id.tv_name, "资格认定证照");
                                }else {
                                    helper.setText(R.id.tv_name, "营业执照");
                                }
                            }
                        } else if (helper.getLayoutPosition() == 1) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_charge);
                                helper.setText(R.id.tv_name, "手持身份证正面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "手持身份证正面");
                            }


                        } else if (helper.getLayoutPosition() == 2) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_card_face);
                                helper.setText(R.id.tv_name, "身份证正面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "身份证正面");
                            }

                        } else if (helper.getLayoutPosition() == 3) {
                            if (item.getPath() == null || item.getPath().isEmpty()) {
                                helper.setImageResource(R.id.iv_iv, R.drawable.ic_card_back);
                                helper.setText(R.id.tv_name, "身份证反面(暂无)");
                            } else {
                                Glide.with(mContext)
                                        .load(item.getPath()).placeholder(R.drawable.loading)
                                        .into((ImageView) helper.getView(R.id.iv_iv));
                                helper.setText(R.id.tv_name, "身份证反面");
                            }

                        }
                    }
                    break;
            }
        }


        // public class PhotoAdapter extends BaseQuickAdapter<EntPic, BaseViewHolder> {
        //            public PhotoAdapter() {
        //                super(R.layout.product_pic_item, ((EntDetailActivity) getActivity()).getEntPics());
        //            }
        //
        //            @Override
        //            protected void convert(BaseViewHolder helper, EntPic item) {
        //                Glide.with(mContext)
        //                        .load(item.getPath()).placeholder(R.drawable.loading)
        //                        .into((ImageView) helper.getView(R.id.iv_iv));
        //            }
        //        }
    }

}